<template>
  <div class="newsinfo-container">
    <h3 class="title">{{newsinfo.title}}</h3>
    <p class="subtitle">
      <span>发表时间：{{newsinfo.add_time | dateFormat}}</span>
      <span>点击：{{newsinfo.click}}次</span>
    </p>
    <hr/>
    <div class="content">{{newsinfo.content}}</div>
    <!-- 子组件评论区 -->
    <comment-box :id="this.id"></comment-box>
  </div>
</template>
<script>
import {Toast} from "mint-ui"
//导入评论子组件
import comment from '../subcomponents/comment.vue'

export default {
  data() {
    return {
        id:this.$route.params.id,//将URL中挂载的ID值 赋值给id
        newsinfo:{}//异步获取新闻信息
    };
  },
  created() {
    this.getNewsInfo()
  },
  methods:{
      getNewsInfo(){//获取新闻列表
          this.$http.get('./json/newslist.json').then(result => {
              if(result.body.status === 0){
                  //成功，把数据保存到data上
                  var messageList = result.body.message //获取所有新闻列表
                  var index = messageList.findIndex(item=>{//获取id对应的对象序号
                      if(item.id == this.id){
                          return true;
                      }
                  })
                  this.newsinfo = messageList[index]

              }else{
                  Toast('获取新闻信息失败！')
              }
          })
      }
  },
  components:{//用来注册子组件的节点
    'comment-box':comment
  }
};
</script>
<style lang="less">
.newsinfo-container {
  padding:0 10px;
  .title{
    font-size: 16px;
    text-align: center;
    line-height:30px;
    color:#f50000;
  }
  .subtitle {
    font-size: 12px;
    color: #226aff;
    display: flex;
    justify-content: space-between;
  }
  .content{
    font-size:12px;
    img{
      width:100%;/**此处需要将css设置中的 scoped 去掉*/
    }
  }
  
}
</style>